/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.ProcurementBP;

import java.util.Collection;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import merlion.common.entity.DocFile;
import merlion.common.session.stateless.FileManagerSessionLocal;
import merlion.frm.bpm.entity.SupplierReceipt;
import merlion.frm.bpm.session.ProcurementBPSessionBeanLocal;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author Kenn
 */
@ManagedBean(name = "srManagerBean")
@ViewScoped
public class SupplierReceiptManagerBean {

    @EJB
    private ProcurementBPSessionBeanLocal pbpSessionBean;
    @EJB
    private FileManagerSessionLocal fileManager;
    private SupplierReceipt receipt;
    private Collection<SupplierReceipt> receipts;
    private String statusMsg;
    private String supplierInvoiceNo;
    
    //--------------------------------------------------------------------------
    //                              Constructor
    //--------------------------------------------------------------------------
    public SupplierReceiptManagerBean() {
        
        statusMsg = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("statusMsg");
        receipt = (SupplierReceipt) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("receipt");
        
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("statusMsg");
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("receipt");
        
        if(receipt == null){
            receipt = new SupplierReceipt();
        }
    }
    
    //--------------------------------------------------------------------------
    //                  Supplier Receipt Manager Bean Methods
    //--------------------------------------------------------------------------
    
    public void handleFileUploadSupplierReceipt(FileUploadEvent event) {
        
        UploadedFile file = event.getFile();
        //application code
        DocFile myDoc = new DocFile(file.getFileName(), file.getContentType(), file.getContents());
        receipt = fileManager.uploadFileIntoSupplierReceipt(myDoc, receipt);
        if (receipt != null) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_INFO, "Supplier Receipt uploaded successfully", ""));
        } else {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Supplier Receipt upload failed", ""));
        }
    }
    
    public String saveReceipt(){
        
        boolean valid = true;
        if(receipt.getUploadedImage() == null){
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Supplier Receipt File must be uploaded", ""));
            valid = false;
        }
        if(receipt.getReceiptAmount() == 0){
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Supplier Receipt amount cannot be zero", ""));
            valid = false;
        }
        //Receipt amount must match with supplier payment record
        if(receipt.getReceiptAmount() != pbpSessionBean.getSupplierInvoiceByIdStr(supplierInvoiceNo).getInvoiceAmount()){
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Supplier Receipt amount does not match to supplier payment record amount", ""));
            valid = false;
        }
        if(!valid){
            return null;
        }
        
        receipt = pbpSessionBean.saveSupplierReceipt(receipt);
        if(receipt != null){
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("statusMsg", "Supplier Receipt saved successfully.");
        } else {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("statusMsg", "Supplier Receipt cannot be saved. Pleaase try again.");
        }
        return "storeReceiptStatus?faces-redirect=true";
    }
    
    public String viewSupplierReceipt(){
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("receipt", receipt);
        return "viewReceiptDisplay?faces-redirect=true";
    }
    
    public String editSupplierReceipt(){
        return null;
    }
    
    //--------------------------------------------------------------------------
    //                              Getter and Setter
    //--------------------------------------------------------------------------

    public SupplierReceipt getReceipt() {
        return receipt;
    }

    public void setReceipt(SupplierReceipt receipt) {
        this.receipt = receipt;
    }

    public Collection<SupplierReceipt> getReceipts() {
        return pbpSessionBean.getAllSupplierReceipt();
    }

    public void setReceipts(Collection<SupplierReceipt> receipts) {
        this.receipts = receipts;
    }

    public String getStatusMsg() {
        return statusMsg;
    }

    public void setStatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }

    public String getSupplierInvoiceNo() {
        return supplierInvoiceNo;
    }

    public void setSupplierInvoiceNo(String supplierInvoiceNo) {
        this.supplierInvoiceNo = supplierInvoiceNo;
    }
    
}
